//
//  SJZTabBar.m
//  SJZBusiness
//
//  Created by 彭利民 on 2018/4/26.
//  Copyright © 2018年 bwton. All rights reserved.
//

#import "SJZTabBar.h"
#import "UIView+SJZLayout.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

@implementation SJZTabBar



// 这里可以做一些初始化设置
-(instancetype)initWithFrame:(CGRect)frame {
    if(self = [super initWithFrame:frame]) {
        [self setUpInit];
    }
    return self;
}


- (void)setUpInit {
    
    // 设置样式, 去除tabbar上面的黑线
    self.barStyle = UIBarStyleBlack;
    
    // 设置tabbar 背景图片
//    NSBundle *currentBundle = [NSBundle bundleForClass:[self class]];
//    NSString *bundleName = [currentBundle.infoDictionary[@"CFBundleName"] stringByAppendingString:@".bundle"];
//    NSString *path = [currentBundle pathForResource:@"tabbar_bg@3x.png" ofType:nil inDirectory:bundleName];
//    UIImage *image = [UIImage imageWithContentsOfFile:path];
    UIImage *image = [UIImage imageNamed:@"tabbar_bg@3x.png"];

    self.backgroundImage = image;
    
}





-(void)layoutSubviews {
    [super layoutSubviews];
    
    // 将中间按钮, 移动到顶部
    //    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    //    [window.rootViewController.view bringSubviewToFront:self.middleBtn];
    
    
    NSInteger count = self.items.count;
    
    // 1. 遍历所有的子控件
    NSArray *subViews = self.subviews;
    
    // 确定单个控件的大小
    CGFloat btnW = self.width / count;
    CGFloat btnH = self.height;
    CGFloat btnY = 0;
    
    NSInteger index = 0;
    for (UIView *subView in subViews) {
        if ([subView isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            //            if (index == count / 2) {
            //                index ++;
            //            }
            
            CGFloat btnX = index * btnW;
            subView.frame = CGRectMake(btnX, btnY, btnW, btnH);
            
            index ++;
        }
    }
    
}


@end
